<?php

namespace App\Models;

use App\Models\Traits\HasPermissionType;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Permission extends Model
{
    use HasFactory, SoftDeletes, HasPermissionType;

    protected $fillable = [
        'name',
        'code',
        'type',
        'action',
        'resource',
        'menu_id',
        'remark',
    ];

    protected $casts = [
        'menu_id' => 'integer',
    ];

    public function menu()
    {
        return $this->belongsTo(Menu::class);
    }

    public function roles()
    {
        return $this->belongsToMany(Role::class, 'role_permissions');
    }

    /**
     * 检查是否为菜单权限
     *
     * @return bool
     */
    public function isMenuPermission(): bool
    {
        return $this->type === self::TYPE_MENU;
    }

    /**
     * 检查是否为按钮权限
     *
     * @return bool
     */
    public function isButtonPermission(): bool
    {
        return $this->type === self::TYPE_BUTTON;
    }

    /**
     * 检查是否为数据权限
     *
     * @return bool
     */
    public function isDataPermission(): bool
    {
        return $this->type === self::TYPE_DATA;
    }
}

